/**
 * Copyright 2000 self.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 *
 * @author Ashin Wimalajeewa
 */
package self.amigo.editor.sheet.uml;

import java.awt.BorderLayout;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import self.amigo.editor.APropertySheet;
import self.amigo.editor.cmd.PropertiesChange;
import self.amigo.elem.uml.ArchetypeUtils;
import self.amigo.elem.uml.AuxilaryCompartmentFeature;
import self.amigo.elem.uml.NameAndCompositionCompartmentsView;
import self.amigo.elem.uml.NameCompartmentFeature;
import self.amigo.elem.uml.PackageView;
import self.awt.layout.VerticalFlowLayout;
import self.gee.IEditableElement;
import self.gee.editor.ICommand;
import self.gee.editor.IPropertySheet;

public class NameAndCompositionCompartmentsSheet extends APropertySheet implements IPropertySheet {
	
	/**
	 * If the internal state of this class ever changes in such a way that it can't be defaulted,
	 * then the {@link #serialVersionUID} should be incremented to ensure serialized instances cleanly fail.  
	 */
	private static final long serialVersionUID = 1;
	
  JTabbedPane nameCompViews = new JTabbedPane();
  BorderLayout borderLayout1 = new BorderLayout();
  JPanel generalPanel = new JPanel();
  JLabel nameLabel = new JLabel();
  VerticalFlowLayout verticalFlowLayout1 = new VerticalFlowLayout();
  JTextField name = new JTextField();
  JLabel stereotypeLabel = new JLabel();
  JTextField stereotype = new JTextField();
  JLabel namespaceLabel = new JLabel();
  JTextField namespace = new JTextField();
  JPanel compartmentView = new JPanel();
  JCheckBox compIsVisible = new JCheckBox();
  VerticalFlowLayout verticalFlowLayout3 = new VerticalFlowLayout();
  JScrollPane compScroller = new JScrollPane();
  JTextArea details = new JTextArea();
  JTextField aidLink = new JTextField();
  JLabel aidLinkLabel = new JLabel();
  BorderLayout borderLayout2 = new BorderLayout();
  JScrollPane nameHost = new JScrollPane();
  JPanel      namePane = new JPanel();
  BorderLayout nameLayout = new BorderLayout();
  JScrollPane compartmentHost = new JScrollPane();
  JPanel      compartmentPane = new JPanel();
  BorderLayout compartmentLayout = new BorderLayout();
  JLabel archetypeLabel = new JLabel();
  JComboBox archetypeColor = new JComboBox();

  private IEditableElement view;
  private NameCompartmentFeature     general;
  private AuxilaryCompartmentFeature composition;

  public NameAndCompositionCompartmentsSheet() {
    try {
      jbInit();
      archetypeColor.setModel( ArchetypeUtils.createArchetypeModel() );
    } catch(Exception e) {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    this.setLayout(borderLayout1);
    nameLabel.setDisplayedMnemonic('N');
    nameLabel.setLabelFor(name);
    nameLabel.setText("name");
    name.setFocusAccelerator('N');

    stereotypeLabel.setText("stereotype");
    archetypeLabel.setText("archetype color");
    namespaceLabel.setText("namespace");
    compIsVisible.setText("visible");
//    compIsVisible.setHorizontalTextPosition(SwingConstants.LEADING);
    details.setRows(10);
    details.setWrapStyleWord(true);
    aidLinkLabel.setDisplayedMnemonic('0');
    aidLinkLabel.setText("aid");

    namePane.setLayout(verticalFlowLayout1);
    namePane.add(nameLabel, null);
    namePane.add(name, null);
    namePane.add(stereotypeLabel, null);
    namePane.add(stereotype, null);
    namePane.add(archetypeLabel, null);
    namePane.add(archetypeColor, null);
    namePane.add(namespaceLabel, null);
    namePane.add(namespace, null);
    namePane.add(aidLinkLabel, null);
    namePane.add(aidLink, null);

    nameHost.getViewport().add( namePane, null );
    generalPanel.setLayout(nameLayout);
    generalPanel.add( nameHost, BorderLayout.CENTER );

    compScroller.getViewport().add(details, null);
    compartmentPane.setLayout(verticalFlowLayout3);
    compartmentPane.add(compIsVisible, null);
    compartmentPane.add(compScroller, null);

    compartmentHost.getViewport().add( compartmentPane, null );
    compartmentView.setLayout(compartmentLayout);
    compartmentView.add( compartmentHost, BorderLayout.CENTER );

    this.add(nameCompViews, BorderLayout.CENTER);
    nameCompViews.add(generalPanel, "General");
    nameCompViews.add(compartmentView, "Compartment");
  }

  public void inspect( Object toDisplay ) {
    if ( toDisplay instanceof NameAndCompositionCompartmentsView ) {
      view = (IEditableElement) toDisplay;
      revert();
    } else
      throw new RuntimeException( "Invalid Graphic element to display:" + toDisplay );
  }

  public void accept() {
    if ( view == null )
      return;
    general.name = name.getText();

    general.stereotype = stereotype.getText();
    general.archetypeColor = archetypeColor.getSelectedIndex();
    general.nameSpace = namespace.getText();
    general.aidLink = aidLink.getText();

    composition.visible = compIsVisible.isSelected();
    composition.details = details.getText();

    PropertiesChange cmd = new PropertiesChange( editor, view, store );
    editor.executeCommand( ICommand.ET_EXECUTE, cmd );
  }

  public void revert() {
    if ( view == null )
      return;
    store.clear();
    view.getProperties( store );

    general = (NameCompartmentFeature) store.get( PackageView.NAMECOMPARTMENT_PROP );
    composition = (AuxilaryCompartmentFeature) store.get( PackageView.COMPOSITIONCOMPARTMENT_PROP );

    name.setText( general.name );
    stereotype.setText( general.stereotype );
    archetypeColor.setSelectedIndex( general.archetypeColor );
    namespace.setText( general.nameSpace );
    aidLink.setText( general.aidLink );

    compIsVisible.setSelected( composition.visible );
    details.setText( composition.details );
  }

  public Object getObjectUnderInspection() {
    return view;
  }
//  static public void main( String[] args ) {
//    JFrame form = new JFrame( "test" );
//    NameAndCompositionCompartmentsSheet sheet = new NameAndCompositionCompartmentsSheet();
//    form.getContentPane().add( sheet );
//    form.show();
//  }
}
